/*
 * Powered By XY
 * Since 2020
 */

package com.xy.biz.rs.controller;

import java.util.Collection;
import java.util.List;

import com.xy.lang.domain.Page;
import com.xy.lang.domain.Result;
import com.xy.lang.validation.SaveChecks;
import com.xy.lang.validation.UpdateChecks;
import com.xy.lang.validation.annotation.Validatable;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.xy.biz.rs.domain.resp.GetEquipmentResponse;
import com.xy.biz.rs.domain.req.GetEquipmentRequest;
import com.xy.biz.rs.domain.req.ListEquipmentRequest;
import com.xy.biz.rs.domain.req.SaveEquipmentRequest;
import com.xy.biz.rs.domain.query.EquipmentQuery;
import com.xy.biz.rs.domain.converter.EquipmentConverter;
import com.xy.biz.rs.client.EquipmentClient;
import com.xy.biz.rs.service.EquipmentService;

/**
 *
 *
 * @author yzh
 * @date 2021-4-13
 */
@Slf4j
@RestController
@RequestMapping("/api/rs/equipment")
public class EquipmentController implements EquipmentClient {

	@Autowired
	private EquipmentService equipmentService;

	@Validatable(groups = {SaveChecks.class})
	@PostMapping(value = "/save")
	public Result<?> saveEquipment(@RequestBody SaveEquipmentRequest request) {
		return equipmentService.saveEquipment(request);
	}

	@Validatable(groups = {SaveChecks.class})
	@PostMapping(value = "/save_all")
	public Result<?> saveAllEquipment(@RequestBody Collection<SaveEquipmentRequest> collection) {
		equipmentService.saveAllEquipment(EquipmentConverter.toBO(collection));
		return Result.success();
	}

	@Validatable(groups = {UpdateChecks.class})
	@PostMapping(value = "/update")
	public Result<?> updateEquipment(@RequestBody SaveEquipmentRequest request) {
		return equipmentService.updateEquipment(request);
	}

	@Validatable(groups = {UpdateChecks.class})
	@PostMapping(value = "/update_status")
	public Result<?> updateEquipmentStatus(@RequestBody SaveEquipmentRequest request) {
		return equipmentService.updateEquipmentStatus(request);
	}

	@Validatable
	@PostMapping(value = "/delete")
	public Result<?> deleteEquipment(@RequestBody GetEquipmentRequest request) {
		equipmentService.deleteEquipment(request.getEquipmentId());
		return Result.success();
	}

	@Validatable
	@PostMapping(value = "/get")
	@Override
	public Result<GetEquipmentResponse> getEquipment(@RequestBody GetEquipmentRequest request) {
		return Result.success(EquipmentConverter.toResponse(equipmentService.getEquipment(request.getEquipmentId())));
	}

	@PostMapping(value = "/list_by_ids")
	@Override
	public Result<List<GetEquipmentResponse>> listEquipmentById(@RequestBody Collection<Long> collection) {
		return Result.success(EquipmentConverter.toResponse(equipmentService.listEquipmentById(collection)));
	}

	@PostMapping(value = "/list")
	@Override
	public Result<List<GetEquipmentResponse>> listEquipment(@RequestBody ListEquipmentRequest request) {
		return Result.success(EquipmentConverter.toResponse(equipmentService.listEquipment(EquipmentConverter.toQuery(request))));
	}

	@PostMapping(value = "/count")
	@Override
	public Result<Integer> countEquipment(@RequestBody ListEquipmentRequest request) {
		return Result.success(equipmentService.countEquipment(EquipmentConverter.toQuery(request)));
	}

	@PostMapping(value = "/page")
	@Override
	public Result<Page<GetEquipmentResponse>> pageEquipment(@RequestBody ListEquipmentRequest request) {
		EquipmentQuery query = EquipmentConverter.toQuery(request);
		List<GetEquipmentResponse> list = EquipmentConverter.toResponse(equipmentService.listEquipment(query));
		int count = equipmentService.countEquipment(query);
		return Result.success(new Page<>(list, count));
	}

}
